#include "stdafx.h"
#include "GMap.h"

GMap::GMap() 
{    
	Surf_Tileset = NULL;
}

GMap::~GMap()
{
}

bool GMap::OnLoad(char* File)
{
	TileList.clear();

	FILE* FileHandle = fopen(File, "r");

	if(FileHandle == NULL)
	{
		return false;
	}

	for(int Y = 0; Y < MAP_HEIGHT; Y++)
	{
		for(int X = 0; X < MAP_WIDTH; X++)
		{
			GTile tempTile;

			fscanf(FileHandle, "%d:%d", &tempTile.TileID, &tempTile.TypeID);

			//Store tile in the TileList
			TileList.push_back(tempTile);
		}
		fscanf(FileHandle, "\n");
	}
	fclose(FileHandle);

	return true;
}

void GMap::OnRender(SDL_Surface* Surf_Display, int MapX, int MapY)
{
	if(Surf_Tileset == NULL)
		return;

	int TilesetWidth = Surf_Tileset->w / TILE_SIZE;
	int TilesetHeight = Surf_Tileset->h / TILE_SIZE;

	int ID = 0;

	for(int Y = 0; Y < MAP_HEIGHT; Y++)
	{
		for(int X = 0; X < MAP_WIDTH; X++)
		{
			if(TileList[ID].TypeID == TILE_TYPE_NONE)
			{               
				ID++;         
				continue;        
			}

			int tX = MapX + (X * TILE_SIZE);  
			int tY = MapY + (Y * TILE_SIZE);

			int TilesetX = (TileList[ID].TileID % TilesetWidth) * TILE_SIZE;
			int TilesetY = (TileList[ID].TileID / TilesetWidth) * TILE_SIZE;

			SDLSurface::OnDraw(Surf_Display, Surf_Tileset, tX, tY, TilesetX, TilesetY, TILE_SIZE, TILE_SIZE);

			ID++;
		}
	}
}

GTile* GMap::GetTile(int X, int Y) 
{
	int ID = 0;

	ID = X / TILE_SIZE;
	ID = ID + (MAP_WIDTH * (Y / TILE_SIZE));

	if(ID < 0 || ID >= TileList.size()) {
		return NULL;
	}

	return &TileList[ID];
}
